<template>
  <el-container class="full-container">
    <el-aside width="220px" class="aside-left">
      <div class="aside-header">上传服务器</div>
      <ul class="aside-left-ul">
        <li
          v-for="(item) in Northbound"
          :key="item.id"
          :class="{'active':q.northboundId===item.id}"
          @click="northboundClick(item.id)"
        >
          <span :title="item.id+'-'+item.name">{{ item.id }}-{{ item.name }}</span>
        </li>
      </ul>
    </el-aside>
    <el-container>
      <div class="app-container">
        <div class="filter-container">
          <el-button v-permission="['Admin.Remote.Update']" class="filter-item" type="danger" icon="el-icon-delete" @click="deleteDataCacheAll">删除所有</el-button>
          <el-button
            v-permission="['Admin.Remote.Update']"
            :disabled="itemsChecked.length === 0"
            class="filter-item"
            type="danger"
            icon="el-icon-delete"
            @click="deleteData()"
          >批量删除
          </el-button>
        </div>
        <el-table
          :key="tableKey"
          v-loading="tableLoading"
          :data="dataElements"
          border
          fit
          highlight-current-row
          style="width: 100%;"
          @selection-change="selectionChange"
        >
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column label="位号" prop="source" min-width="90"></el-table-column>
          <el-table-column label="事件类型" prop="category" align="center" min-width="90px"></el-table-column>
          <el-table-column label="级别" prop="severity" align="center" min-width="90px"></el-table-column>
          <el-table-column label="状态" align="center" min-width="90px">
            <template slot-scope="{row}">
              {{ transState(row.conditionState) }}
            </template>
          </el-table-column>
          <el-table-column label="报警类型" prop="conditionName" align="center" min-width="90"></el-table-column>
          <el-table-column label="数据源名称" prop="equipmentName" align="center" min-width="90"></el-table-column>
          <el-table-column label="当前值" prop="currentValue" align="center" min-width="90"></el-table-column>
          <el-table-column label="阈值" prop="threshold" align="center" min-width="90"></el-table-column>
          <el-table-column label="告警信息" prop="message" align="center" min-width="90"></el-table-column>
          <el-table-column label="触发时间" align="center" min-width="90">
            <template slot-scope="scope">
              <span>{{ transDate(scope.row.activeTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button v-permission="['Admin.Remote.Update']" size="mini" type="danger" @click="deleteData(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="totalCount > 0"
          :total="totalCount"
          :page-index.sync="q.page"
          :page-size.sync="q.size"
          @pagination="getNorthboundAlarmCache"
        />
      </div>
    </el-container>
  </el-container>
</template>

<script>
  import {
    getShortNorthbound,
    getNorthboundAlarmCache,
    deleteAlarmCache,
    deleteAlarmCacheAll
  } from "@/api/northbound";
  import Pagination from "@/components/Pagination";
  import _ from "lodash";
  import moment from "moment";

  export default {
    components: {
      'pagination': Pagination
    },
    data() {
      return {
        isSubmitLoading: false,
        Northbound: [],
        tableLoading: false,
        tableKey: 0,
        dataElements: [],
        itemsChecked: [],
        q: {
          northboundId: undefined,
          page: 1,
          size: 10
        },
        totalCount: 0
      };
    },
    computed: {},
    created() {
      this.getShortNorthbound();
    },
    methods: {
      getShortNorthbound() {
        getShortNorthbound().then(result => {
          this.Northbound = result;
          // this.q.northboundId = result[0].id;
          this.getNorthboundAlarmCache();
        });
      },
      northboundClick(id) {
        this.q.northboundId = id;
        this.getNorthboundAlarmCache();
      },
      getNorthboundAlarmCache() {
        this.tableLoading = true;
        getNorthboundAlarmCache(this.q).then(result => {
          this.dataElements = result.content;
          this.totalCount = result.totalElements;
          this.tableLoading = false;
        });
      },
      indexMethod(index) {
        return this.q.size * (this.q.page - 1) + index + 1;
      },
      selectionChange(selection) {
        this.itemsChecked = selection;
      },
      transDate(timestamp) {
        return moment(timestamp * 1000).format('YYYY-MM-DD HH:mm:ss');
      },
      transState(conditionState) {
        let returnVal = '';
        switch (conditionState) {
          case 0:
            returnVal = '报警';
            break;
          case 1:
            returnVal = '消警';
            break;
        }
        return returnVal;
      },
      deleteData(id) {
        let selectedIds = [];
        if (id) {
          selectedIds = [id];
        } else {
          selectedIds = this.itemsChecked.map(item => _.parseInt(item.id));
          if (selectedIds.length <= 0) {
            this.$message({ message: "至少选中一条记录", type: "warning" });
            return;
          }
        }

        this.$confirm("删除后不可恢复， 是否继续?", "确定执行删除操作吗？", {
          type: "warning"
        })
          .then(() => {
            deleteAlarmCache(selectedIds).then(() => {
              this.getNorthboundAlarmCache();
              this.$message({ message: "删除成功", type: "success" });
            });
          })
          .catch(() => {
          });
      },
      deleteDataCacheAll() {
        this.$confirm("删除后不可恢复，是否继续?", "确定执行全部删除操作吗？", {
          type: "warning"
        })
          .then(() => {
            deleteAlarmCacheAll(this.q.northboundId).then(() => {
              this.getNorthboundAlarmCache();
              this.$message({ message: "删除成功", type: "success" });
            });
          })
          .catch(() => {
          });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .full-container {
    min-height: calc(100vh - 50px);
  }

  .aside-left {
    margin-bottom: 0;
  }

  .aside-header {
    display: flex;
    align-items: center;
    font-weight: bold;

    &:before {
      content: '';
      display: inline-block;
      margin-right: 5px;
      width: 4px;
      height: 16px;
      background-color: #000000;
    }
  }

  .aside-left-ul {
    margin: 0;
    list-style: none;
    padding-left: 0;

    li {
      position: relative;
      display: flex;
      align-items: center;
      line-height: 40px;
      cursor: pointer;
      border-bottom: 1px dashed #ccc;

      &:before {
        display: inline-block;
        margin-right: 10px;
        content: '';
        width: 10px;
        height: 10px;
        border: 1px solid #000;
      }

      &:after {
        position: absolute;
        top: 12px;
        z-index: 1;
        right: 0;
        content: "\E6E0";
        font-family: 'element-icons' !important;
        speak: none;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        vertical-align: baseline;
        display: inline-block;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
      }

      &.active {
        color: #1890ff;
        border-color: #1890ff;
        border-bottom-style: solid;

        &:before {
          background-color: #1890ff;
          border-color: #1890ff;
        }
      }

      & > span {
        display: block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
  .app-container{
    flex: 1;
    overflow: hidden;
  }
</style>
